package com.ts.mapper.achievement;

import com.ts.entity.Paper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PaperMapper {

    /**
     * 新增论文（主键自增返回）
     * @param paper 论文实体
     */
    @Insert("INSERT INTO `tianshi-college-teacher-system-test`.ts_paper " +
            "(achievement_type, is_first_author, is_first_in_unit, paper_name, author, " +
            "author_rank, is_corresponding_author, is_teacher, research_category, paper_category, " +
            "subject_category, achievement_source_name, achievement_source_level, published_journal, " +
            "publish_time, volume_issue_number, page_range, doi, inclusion_status, " +
            "is_industry_joint_publication, is_local_joint_publication, is_international_joint_publication, " +
            "is_interdisciplinary_paper, remark, evidence_material, create_by, create_time, " +
            "update_by, update_time) " +
            "VALUES " +
            "(#{achievementType}, #{isFirstAuthor}, #{isFirstInUnit}, #{paperName}, #{author}, " +
            "#{authorRank}, #{isCorrespondingAuthor}, #{isTeacher}, #{researchCategory}, #{paperCategory}, " +
            "#{subjectCategory}, #{achievementSourceName}, #{achievementSourceLevel}, #{publishedJournal}, " +
            "#{publishTime}, #{volumeIssueNumber}, #{pageRange}, #{doi}, #{inclusionStatus}, " +
            "#{isIndustryJointPublication}, #{isLocalJointPublication}, #{isInternationalJointPublication}, " +
            "#{isInterdisciplinaryPaper}, #{remark}, #{evidenceMaterial}, #{createBy}, #{createTime}, " +
            "#{updateBy}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void save(Paper paper);

    /**
     * 根据ID删除论文
     * @param id 论文ID
     * @return 影响行数（1-成功，0-失败）
     */
    @Delete("DELETE FROM `tianshi-college-teacher-system-test`.ts_paper WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 根据ID更新论文（全字段更新）
     * @param paper 论文实体（需包含ID）
     * @return 影响行数（1-成功，0-失败）
     */
    @Update("UPDATE `tianshi-college-teacher-system-test`.ts_paper SET " +
            "achievement_type = #{achievementType}, " +
            "is_first_author = #{isFirstAuthor}, " +
            "is_first_in_unit = #{isFirstInUnit}, " +
            "paper_name = #{paperName}, " +
            "author = #{author}, " +
            "author_rank = #{authorRank}, " +
            "is_corresponding_author = #{isCorrespondingAuthor}, " +
            "is_teacher = #{isTeacher}, " +
            "research_category = #{researchCategory}, " +
            "paper_category = #{paperCategory}, " +
            "subject_category = #{subjectCategory}, " +
            "achievement_source_name = #{achievementSourceName}, " +
            "achievement_source_level = #{achievementSourceLevel}, " +
            "published_journal = #{publishedJournal}, " +
            "publish_time = #{publishTime}, " +
            "volume_issue_number = #{volumeIssueNumber}, " +
            "page_range = #{pageRange}, " +
            "doi = #{doi}, " +
            "inclusion_status = #{inclusionStatus}, " +
            "is_industry_joint_publication = #{isIndustryJointPublication}, " +
            "is_local_joint_publication = #{isLocalJointPublication}, " +
            "is_international_joint_publication = #{isInternationalJointPublication}, " +
            "is_interdisciplinary_paper = #{isInterdisciplinaryPaper}, " +
            "remark = #{remark}, " +
            "evidence_material = #{evidenceMaterial}, " +
            "create_by = #{createBy}, " +
            "create_time = #{createTime}, " +
            "update_by = #{updateBy}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateById(Paper paper);

    /**
     * 根据ID查询论文
     * @param id 论文ID
     * @return 论文实体（null表示不存在）
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_paper WHERE id = #{id}")
    Paper selectById(Long id);

    /**
     * 查询所有论文
     * @return 论文列表
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_paper")
    List<Paper> selectAll();

    /**
     * 分页查询论文
     * @param offset 偏移量（(pageNum-1)*pageSize）
     * @param limit 每页条数
     * @return 分页论文列表
     */
    @Select("SELECT * FROM `tianshi-college-teacher-system-test`.ts_paper LIMIT #{offset}, #{limit}")
    List<Paper> selectByPage(@Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 查询总条数（用于分页）
     * @return 总记录数
     */
    @Select("SELECT COUNT(*) FROM `tianshi-college-teacher-system-test`.ts_paper")
    Long selectTotalCount();
}